/*****************************************************************************************
 * FILE NAME: drv_can                      COPYRIGHT (c) cos 2024
 * REVISION:  1.0.0                                     All Rights Reserved
 * DESCRIPTION:
 * =======================================================================================
 * UPDATE HISTORY:
 * REV      AUTHOR      DATE
 * v1.0.0   Tuxin       2024.10.14
 * DESCRIPTION OF CHANGE:
 ****************************************************************************************/
#ifndef __DRV_CAN_H
#define __DRV_CAN_H

//----------------------------------------------------------------------------------------
// @Project Includes
#include "typedefs.h"

//----------------------------------------------------------------------------------------
// @Global Macro Define in the module
#define  CAN_BAUDRATE_1M             	(1)
#define  CAN_BAUDRATE_500K          	(2)
#define  CAN_BAUDRATE_250K           	(3)
#define  CAN_BAUDRATE_125K           	(4)
#define  CAN_BAUDRATE                	(CAN_BAUDRATE_250K)
#if(CAN_BAUDRATE==CAN_BAUDRATE_1M)
#define   CAN_BIT_RSJW              	CAN_RSJW_1tq
#define   CAN_BIT_BS1               	CAN_TBS1_5tq
#define   CAN_BIT_BS2               	CAN_TBS2_2tq
#define   CAN_BAUDRATEPRESCALER     	2 
#elif(CAN_BAUDRATE==CAN_BAUDRATE_500K)
#define   CAN_BIT_RSJW              	CAN_RSJW_1tq
#define   CAN_BIT_BS1               	CAN_TBS1_10tq
#define   CAN_BIT_BS2               	CAN_TBS2_5tq
#define   CAN_BAUDRATEPRESCALER     	2
#elif(CAN_BAUDRATE==CAN_BAUDRATE_250K)
#define   CAN_BIT_RSJW              	CAN_RSJW_1tq
#define   CAN_BIT_BS1               	CAN_TBS1_10tq
#define   CAN_BIT_BS2               	CAN_TBS2_5tq
#define   CAN_BAUDRATEPRESCALER     	4 
#elif(CAN_BAUDRATE==CAN_BAUDRATE_125K)
#define   CAN_BIT_RSJW              	CAN_RSJW_1tq
#define   CAN_BIT_BS1               	CAN_TBS1_10tq
#define   CAN_BIT_BS2               	CAN_TBS2_5tq
#define   CAN_BAUDRATEPRESCALER     	8
#endif
#define  CAN_TXDLC_8                  ((uint8_t)8)
#define  CAN_FILTERNUM0              	((uint8_t)0)

#define  CAN_FILTER_STDID(STDID)     	((STDID&0x7FF)<<5)
#define  CAN_FILTER_EXTID_H(EXTID)    ((uint16_t)  (((EXTID<<3)|0x04)>>16) )
#define  CAN_FILTER_EXTID_L(EXTID)    ((uint16_t)  (( EXTID<<3)|0x04) )

#define  CAN_STD_ID_H_MASK_CARE       (0xFFE0)
#define  CAN_STD_ID_L_MASK_CARE       (0x0000)
#define  CAN_STD_ID_H_MASK_DONT_CARE  (0x0000)
#define  CAN_STD_ID_L_MASK_DONT_CARE  (0x0000)

#define  CAN_EXT_ID_H_MASK_CARE       (0xFFFF)
#define  CAN_EXT_ID_L_MASK_CARE       (0xFFF8)
#define  CAN_EXT_ID_H_MASK_DONT_CARE  (0x0000)
#define  CAN_EXT_ID_L_MASK_DONT_CARE  (0x0000)

/*the conifg start*/
#define CAN_RX_PIN                 		(GPIO_PIN_11)
#define CAN_TX_PIN                 		(GPIO_PIN_12)
#define CAN_RX_PORT            				(GPIOA)
#define CAN_TX_PORT            				(GPIOA)
#define CAN_GPIO_CLK     							RCC_APB2_PERIPH_GPIOA
#define CAN_MAX_TX_SIZE    	        	(100)
#define CAN_MAX_RX_SIZE    	        	(160)
//----------------------------------------------------------------------------------------
// @Global Type defines in the module
typedef struct stc_can
{
    CanTxMessage canTxMessage[CAN_MAX_TX_SIZE];
    vuint8_t vu8canTxMQPushLen;/*发送写入消息*/
    vuint8_t vu8canTxMQPopLen;/*发送弹出消息*/   
    CanRxMessage canRxMessage[CAN_MAX_RX_SIZE];
    vuint8_t vu8canRxMQPushLen;/*接收写入消息*/
    vuint8_t vu8canRxMQPopLen;/*接收弹出消息*/
	
    uint16_t u16tx50msCnt;/*50ms计时器*/
    uint16_t u16tx100msCnt;/*100ms计时器*/
    uint16_t u16tx200msCnt;/*200ms计时器*/
    uint16_t u16tx500msCnt;/*500ms计时器*/
    uint16_t u16tx1000msCnt;/*1000ms计时器*/
    uint16_t u16tx2000msCnt;/*2000ms计时器*/		
	
	
} stc_can_t;

//----------------------------------------------------------------------------------------
// @Export global Variables declare in the module
extern stc_can_t g_stccan;

//----------------------------------------------------------------------------------------
// @Export declaration of functions in the module
uint8_t drv_can_txMessage(CAN_Module* CANx,CanTxMessage* TxMessage);
void drv_can_init(void);
void drv_can_10msInt(void);
void drv_can_sleep(void);
void drv_can_wakeup(void);

#endif

// =======================================================================================
// End of file.
// =======================================================================================
